package com.lb.activity.sys.service.impl;

import com.lb.activity.mapper.UserRoleMapper;
import com.lb.activity.po.UserRole;
import com.lb.activity.po.UserRoleExample;
import com.lb.activity.sys.service.UserRoleService;
import com.lb.activity.util.SnowflakeIdWorker;
import com.lb.activity.util.emumdata.DataEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 001030
 */
@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private SnowflakeIdWorker idWorker;

    @Override
    public List<UserRole> getUserRoles(Long id) {
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUseridEqualTo(id);
        List<UserRole> storeUserRoles = userRoleMapper.selectByExample(example);
        return storeUserRoles;
    }

    /**
     * 分配用户和角色的关系
     * @param userId
     * @param roles
     */
    @Override
    public void addUserRoles(long userId, String[] roles) {
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUseridEqualTo(userId);
        userRoleMapper.deleteByExample(example);
        for(String roleId:roles){
            if(!(DataEnum.INSERT_STATUS.getStatus()+"").equals(roleId)){
                UserRole userRole = new UserRole();
                userRole.setId(idWorker.nextId());
                userRole.setUserid(userId);
                userRole.setRoleid(Long.parseLong(roleId));
                userRole.setStatus(Integer.parseInt(DataEnum.INSERT_STATUS.getStatus()+""));
                userRoleMapper.insertSelective(userRole);
            }
        }
    }
}
